package com.goalias.job;


import com.goalias.constant.RedisConstants;
import com.goalias.domain.entity.Article;
import com.goalias.service.IArticleService;
import com.goalias.service.RedisService;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Component
public class RedisRunner implements CommandLineRunner {
    @Resource
    private RedisService redisService;

    @Resource
    private IArticleService articleService;

    @Override
    public void run(String... args) {
        List<Article> articles = articleService.listViewCount();
        Map<String, Integer> viewCountMap = articles.stream()
                .collect(Collectors.toMap(
                        article -> article.getId().toString(),
                        article -> article.getViewCount().intValue()
                ));
        redisService.hmSet(RedisConstants.ARTICLE_VIEW_COUNT_KEY, viewCountMap);
    }
}
